package org.linlinjava.litemall.admin.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.util.CloneUtil;
import org.linlinjava.litemall.admin.vo.SecKillListVo;
import org.linlinjava.litemall.admin.vo.SecKillVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/seckill")
@Validated
@Api(value = "秒杀", tags = "秒杀接口")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdminSeckillController {
    
    private final Log logger = LogFactory.getLog(AdminSeckillController.class);

    private final GoodsService goodsService;

    @GetMapping("/list")
    @ApiOperation("秒杀列表查询")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<LitemallGoods> kills = goodsService.queryKillList(page, limit);
        List<SecKillListVo> killListVos = CloneUtil.clonePage(kills, SecKillListVo.class);
        return ResponseUtil.okList(killListVos);
    }

    private Object validate(LitemallGoods seckill) {
        Integer initialNumber = seckill.getInitialNumber();
        BigDecimal killPrice = seckill.getKillPrice();
        LocalDateTime startTime = seckill.getStartTime();
        LocalDateTime endTime = seckill.getEndTime();
        if (StringUtils.isEmpty(seckill.getId()) || StringUtils.isEmpty(initialNumber) || StringUtils.isEmpty(killPrice)
                || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @PostMapping("/create")
    @ApiOperation("新增秒杀")
    public Object create(@RequestBody LitemallGoods seckill) {
        Object error = validate(seckill);
        if (error != null) {
            return error;
        }
        goodsService.updateById(seckill);
        return ResponseUtil.ok(seckill);
    }

    @GetMapping("/read")
    @ApiOperation("查询秒杀")
    public Object read(@NotNull Integer id) {
        LitemallGoods goods = goodsService.findById(id);
        SecKillVo build = SecKillVo.builder().goodId(goods.getId()).startTime(goods.getStartTime()).endTime(goods.getEndTime()).sortOrder(goods.getSortOrder())
                .number(goods.getNumber()).initialNumber(goods.getInitialNumber()).initialPrice(goods.getRetailPrice())
                .killPrice(goods.getKillPrice()).build();
        return ResponseUtil.ok(build);
    }

    @PostMapping("/update")
    @ApiOperation("更新秒杀")
    public Object update(@RequestBody LitemallGoods seckill) {
        Object error = validate(seckill);
        if (error != null) {
            return error;
        }
        if (goodsService.updateById(seckill) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(seckill);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除秒杀")
    public Object delete(@PathVariable Integer id) {
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        goodsService.deleteKillById(id);
        return ResponseUtil.ok();
    }

}
